export function styleTool(dom: HTMLElement, options: any): HTMLElement {
    try {
        cleanDomsStyle(dom);
        for (const key in options) {
            if (Object.prototype.hasOwnProperty.call(options, key)) {
                let style = options[key];
                if (key === 'fontSize') {
                    style += 'px';
                }
                if (key === 'lineHeight') {
                    style += 'em';
                }
                dom.style[key] = style;
            }
        }
        tableStyle(dom);
        return dom;
    } catch (error) {

    }
}

function tableStyle(dom: HTMLElement) {
    const tables = dom.querySelectorAll('table');
    if (tables && tables.length) {
        tables.forEach(table => {
            setTableStyle(table);
        });
    } else if (dom.tagName === 'TABLE') {
        setTableStyle(dom as HTMLTableElement);
    }
}

function cleanDomsStyle(dom: HTMLElement) {
    const nodes = dom.querySelectorAll('[style*=font-family]');
    nodes.forEach((node: HTMLElement) => {
        clearStyle(node);
    });
    clearStyle(dom);
}

function clearStyle(node: HTMLElement) {
    node.style.fontFamily = null;
    node.style.lineHeight = null;
    node.style.fontSize = null;
}

function setTableStyle(table: HTMLTableElement) {
    table.setAttribute('border', '1');
    table.setAttribute('cellspacing', '0');
}
